package biolabs.core

object Scheduled {

	private val MAX_LONG_SCHEDULE = new Schedule() {
		override def hasNext() = true
		override def next() = Long.MaxValue
	}

	val NEVER = new Scheduled(MAX_LONG_SCHEDULE, Int.MaxValue) {
		override def perform() = {}
	}
}

/**
 * {@code Scheduled} represents an action or an interaction with a schedule.
 * It is used internally in the generated code.
 */
abstract class Scheduled(schedule: Schedule, private val declarationOrder: Int) extends Comparable[Scheduled] {
	if(!schedule.hasNext())
		throw new IllegalArgumentException("empty schedule")

	private var next = schedule.next()

	def moveOn(): Boolean = {
		if(schedule.hasNext()) {
			next = schedule.next()
			true
		} else {
			false
		}
	}

	def time() = next

	override def compareTo(that: Scheduled): Int = {
		if(time() < that.time())
			-1
		else if(time() > that.time())
			1
		else if(declarationOrder < that.declarationOrder)
			-1
		else if(declarationOrder > that.declarationOrder)
			1
		else
			0
	}

	def perform()
}